package com.xiaoyu.lingdian.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.xiaoyu.lingdian.entity.CoreUser;
import com.xiaoyu.lingdian.entity.CoreUserRegister;
import com.xiaoyu.lingdian.service.CoreUserRegisterService;
import com.xiaoyu.lingdian.service.CoreUserService;
import com.xiaoyu.lingdian.tool.DateUtil;
import com.xiaoyu.lingdian.tool.RandomUtil;
import com.xiaoyu.lingdian.tool.out.ResultMessageBuilder;

/**
 *用户签到
 */
@Controller
@RequestMapping(value="/coreUserRegister")
public class CoreUserRegisterController extends BaseController {
	
	@Autowired
	private CoreUserRegisterService coreUserRegisterService;
	
	@Autowired
	private CoreUserService coreUserService;	
	
	/**
	 *	1）、1-10天，每天签到送1个积分
	 *	2）、11-20天，每次签到送2个积分
	 *	3）、20天以后，每天签到送3个积分
	 *	4）、如断签，原有积分不变，积分重新按照第一天签到计算
	*/
	@RequestMapping(value="/register", method=RequestMethod.POST)
	public void register(String criglMobile, HttpServletResponse response) {
		int userPoints = 1;         //用户本次获得积分数,默认为1分
		int continueSignDays = 0;       //用户连续签到天数
		CoreUserRegister coreUserRegister = new CoreUserRegister();
		coreUserRegister.setCriglMobile(criglMobile);
		
		//先查询用户最近一次的签到记录
		List<CoreUserRegister> coreUserRegisters = coreUserRegisterService.getCoreUserRegisterByName(coreUserRegister);
		if(coreUserRegisters!=null&&coreUserRegisters.size()>0){
			logger.info("用户"+criglMobile+"签到,次数"+coreUserRegisters.size());
			CoreUserRegister coreUserRegisterL = coreUserRegisters.get(0);   //取用户签到记录中最后一条记录
			Date lastSignDate = coreUserRegisterL.getCriglCdate();
			int days = DateUtil.diffDate(new Date(), lastSignDate);
			if(days > 1){  			//断签
				coreUserRegister.setCriglSignDays(1);
				userPoints = 1;
			}else{
				//用户不能在0点至24点重复签到。
				String lastSignDateS = DateUtil.formatDate(null, lastSignDate);
				logger.info("用户"+criglMobile+"签到,上一次签到时间："+lastSignDateS);
				if (lastSignDateS.equals(DateUtil.formatDate(null, new Date()))) {
					logger.info("用户"+criglMobile+"签到,上一次签到时间："+lastSignDateS+",您今天已经过签到！");
				    writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "您今天已经过签到！"), response);
					return;
				}
				
				//没有断签，获取连续签到的天数
				continueSignDays= coreUserRegisterL.getCriglSignDays();
				if(continueSignDays > 20){
					userPoints = 3;
				}else if(continueSignDays > 10 &&continueSignDays < 20){
					userPoints = 2;
				}else if(continueSignDays < 10){
					userPoints = 1;
				}
			}
			
			coreUserRegister.setCriglSignDays(continueSignDays+1);
			coreUserRegister.setCriglScore(userPoints);
			String uuid = RandomUtil.generateString(16);
			coreUserRegister.setCriglUuid(uuid);
			coreUserRegister.setCriglCdate(new Date());
			coreUserRegisterService.insertCoreUserRegister(coreUserRegister);
		}else{  //用户没有签到记录，直接插入一条签到记录
			coreUserRegister.setCriglSignDays(1);
			coreUserRegister.setCriglScore(userPoints);
			String uuid = RandomUtil.generateString(16);
			coreUserRegister.setCriglUuid(uuid);
			coreUserRegister.setCriglCdate(new Date());
			coreUserRegisterService.insertCoreUserRegister(coreUserRegister);
		}
		
		logger.info("用户"+criglMobile+"签到,获取积分:"+userPoints);
		CoreUser coreUser = new CoreUser();
		coreUser.setCrusrMobile(criglMobile);
		CoreUser coreUserN = coreUserService.getCoreUserByMobile(coreUser);
		coreUserN.setCrusrPoints(userPoints); //本次获得积分数,更新用户积分总数
		coreUserService.updateCoreUserPointsAdd(coreUserN);
		
		int syScore = coreUserService.getCoreUserByMobile(coreUserN).getCrusrPoints();
		
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "签到成功！", syScore), response);
		logger.info("[CoreUserRegisterController.register]:end register");
	}	
	
}